﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TUR02
    {

        public TUR02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_USERRIGHT();
            DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = "";
                USERID = "";
            }
            else
            {
                KEY = dwMaster["ROWNID"].ToString();
                USERID = dwMaster["USERID"].ToString();
            }
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            SResult = ((VICC_USERRIGHT)MV).QueryICC_USERRIGHT(dwData, -1);
            MergeData(SResult, false);
            base.QueryCommand(sender, e);
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            ((VICC_USERRIGHT)MV).SetUpNewRow(ref dwData);
            LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "URSEQ", 1);
            dwData["ROWNID"] = LFD;
            dwData["COMPANY"] = app.company;
            Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
            base.NewCommand(sender, e);
        }
        public void Insert(DataRow dwUR)
        {
            ((VICC_USERRIGHT)MV).DeleteICC_USERRIGHT(dwUR, 1);
            ((VICC_USERRIGHT)MV).InsertICC_USERRIGHT(dwUR, app.company);
            TBMain.Rows.InsertAt(dwUR, 0);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ((VICC_USERRIGHT)MV).UpdateICC_USERRIGHT(dwMaster, app.company, 0);
            base.SaveCommand(sender, e);
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "URSEQ", 1);
            dwData["ROWNID"] = LFD;
            Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
            base.CopyCommand(sender, e);
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ((VICC_USERRIGHT)MV).DeleteICC_USERRIGHT(dwMaster, 1);
            string filter = "ROWNID='" + dwMaster["ROWNID"].ToString() + "'";
            DataRow[] rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            base.EditCommand(sender, e);
        }

        public override void OnUSERIDChg(string value)
        {

            base.OnUSERIDChg(value);
        }

        private void Copy2UserCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                USERID = "";
                IsNew = true;
                MessageBox.Show("请先保存当前记录");
                return;
            }
            if (dwMaster == null)
            {
                MessageBox.Show("请选择当前用户所在的任意记录");
                return;
            }
            if (USERID.Trim().Length == 0)
            {
                MessageBox.Show("请输入目标用户");
                return;
            }
            filter = "USERID='" + dwMaster["USERID"].ToString() + "' AND PROG='" + dwMaster["PROG"].ToString() + "'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                dwData = TBMain.NewRow();
                dwData.ItemArray = row.ItemArray;
                dwData["USERID"] = USERID.Trim().PadRight(8);
                dwData["ROWNID"] = VSDDAT.SD22.GETNEXT("0022", "  ", "URSEQ", 1);
                Insert(dwData);
            }
            vwMaster.View.MoveCurrentToFirst();
            USERID = "";
            IsNew = false;
        }

        private void DeleteNDEMOCommand(object sender, RoutedEventArgs e)
        {
            filter = "USERID<>'DEMO   '";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                ((VICC_USERRIGHT)MV).DeleteICC_USERRIGHT(row, 1);
                row.Delete();
            }
            TBMain.AcceptChanges();
        }
        private void DeleteNDEMOAndYCommand(object sender, RoutedEventArgs e)
        {
            filter = "USERID<>'DEMO   ' AND PROCESS='"+"Y".PadRight(10)+"'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                ((VICC_USERRIGHT)MV).DeleteICC_USERRIGHT(row, 1);
                row.Delete();
            }
            TBMain.AcceptChanges();
        }


        private void DeleteDEMOCommand(object sender, RoutedEventArgs e)
        {
            filter = "USERID='DEMO   '";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                ((VICC_USERRIGHT)MV).DeleteICC_USERRIGHT(row, 1);
                row.Delete();
            }
            TBMain.AcceptChanges();
        }

        private void DeleteUserCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                USERID = "";
                IsNew = true;
                MessageBox.Show("请先保存当前记录");
                return;
            }
            if (dwMaster == null)
            {
                MessageBox.Show("请选择当前用户所在的任意记录");
                return;
            }
            if (USERID.Trim().Length == 0)
            {
                MessageBox.Show("请输入目标用户");
                return;
            }
            filter = "USERID='" + USERID + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                ((VICC_USERRIGHT)MV).DeleteICC_USERRIGHT(row, 1);
                row.Delete();
            }
            TBMain.AcceptChanges();
            USERID = "";
            IsNew = false;
        }
        private void Copy2AllUsersCommand(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                MessageBox.Show("请先保存当前记录");
                return;
            }
            if (dwMaster == null)
            {
                MessageBox.Show("请选择当前用户所在的任意记录");
                return;
            }
            filter = "USERID='DEMO    '";
            rows = TBMain.Select(filter);

            foreach (DataRow dwHR in cHR.TBMain.Rows)
            {
                string user = dwHR["USERID"].ToString().Trim().PadRight(8);
                if (user == "DEMO    ") continue;
                foreach (DataRow row in rows)
                {
                    dwData = TBMain.NewRow();
                    dwData.ItemArray = row.ItemArray;
                    dwData["USERID"] = user;
                    dwData["ROWNID"] = VSDDAT.SD22.GETNEXT("0022", "  ", "URSEQ", 1);
                    Insert(dwData);
                }
            }
            vwMaster.View.MoveCurrentToFirst();
            USERID = "";
        }
        private void Copy2Prog(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                PROG = "";
                IsNew = true;
                return;
            }
            if (dwMaster == null)
            {
                MessageBox.Show("请选择当前程序所在的任意记录");
                return;
            }
            filter = "USERID='" + USERID + "' AND PROG='" + dwMaster["PROG"].ToString() + "'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                dwData = TBMain.NewRow();
                dwData.ItemArray = row.ItemArray;
                dwData["PROG"] = PROG;
                dwData["ROWNID"] = VSDDAT.SD22.GETNEXT("0022", "  ", "URSEQ", 1);
                Insert(dwData);
            }
            IsNew = false;
            PROG = "";
        }

        private void Copy1AllUsersCommand(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                MessageBox.Show("请保存当前记录");
                return;
            }
            if (dwMaster == null)
            {
                MessageBox.Show("请选择当前用户所在的任意记录");
                return;
            }


            foreach (DataRow dwHR in cHR.TBMain.Rows)
            {
                string user = dwHR["USERID"].ToString().Trim().PadRight(8);
                if (user == dwMaster["USERID"].ToString().Trim().PadRight(8)) continue; 
                dwData = TBMain.NewRow();
                dwData.ItemArray = dwMaster.ItemArray;
                dwData["USERID"] = user;
                dwData["ROWNID"] = VSDDAT.SD22.GETNEXT("0022", "  ", "URSEQ", 1);
                Insert(dwData); 
            }
            vwMaster.View.MoveCurrentToFirst();
            USERID = "";

        }

        private void Delete1AllUsersCommand(object sender, RoutedEventArgs e)
        {

            if (IsEdit)
            {
                USERID = "";
                MessageBox.Show("请先保存当前记录");
                return;
            }
            if (dwMaster == null)
            {
                MessageBox.Show("请选择当前用户所在的任意记录");
                return;
            }
            filter = "USERID<>'DEMO    ' AND CONTROLS='" + dwMaster["CONTROLS"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                ((VICC_USERRIGHT)MV).DeleteICC_USERRIGHT(row, 1);
                row.Delete();
            }

            TBMain.AcceptChanges();
            USERID = "";
        }


        public void ReplaceControls(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                CONTROLS = "";
                IsNew = true;
                return;
            }
            if (dwMaster == null)
                return;
            filter = "CONTROLS='" + dwMaster["CONTROLS"].ToString() + "'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row["CONTROLS"] = CONTROLS;
                ((VICC_USERRIGHT)MV).UpdateICC_USERRIGHT(row, app.company, 1);
            }
            IsNew = false;
            CONTROLS = "";
        }
    }
}
